﻿using System.Linq;
using System.Text.RegularExpressions;

namespace HostTool.Core
{
    public static class StringExtension
    {
        /// <summary>
        /// 判断字符串中指定字符是否是大写
        /// </summary>
        /// <param name="value"></param>
        /// <param name="position"></param>
        /// <returns></returns>
        public static bool IsUpper(this string value, int position)
        {
            position = position - 1;
            if (string.IsNullOrWhiteSpace(value)) return false;
            char[] chars = value.ToArray();
            if (chars.Count() < position || position < 0) return false;
            char s = chars[position];
            return char.IsUpper(s);
        }

        /// <summary>
        /// 执行string的IsNullOrWhiteSpace方法
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsNull(this string value)
        {
            return string.IsNullOrWhiteSpace(value);
        }

        /// <summary>
        /// 字符串指定位置的字符转换小写
        /// </summary>
        /// <param name="value"></param>
        /// <param name="position"></param>
        /// <returns></returns>
        public static string ConvertLower(this string value, int position)
        {
            position = position - 1;
            if (value.IsNull()) return value;
            char[] chars = value.ToArray();
            if (chars.Count() < position || position < 0) return value;
            chars[position] = char.ToLower(chars[position]);
            return chars.Aggregate("", (current, c) => current + c);
        }

        /// <summary>
        /// 字符串指定位置的字符转换大写
        /// </summary>
        /// <param name="value"></param>
        /// <param name="position"></param>
        /// <returns></returns>
        public static string ConvertUpper(this string value, int position)
        {
            position = position - 1;
            if (value.IsNull()) return value;
            char[] chars = value.ToArray();
            if (chars.Count() < position || position < 0) return value;
            chars[position] = char.ToUpper(chars[position]);
            return chars.Aggregate("", (current, c) => current + c);
        }

        /// <summary>
        /// 查找字符串中相邻字符是否有同时大写,并返回
        /// </summary>
        /// <returns></returns>
        public static string FindAdjacentCharUpper(this string value)
        {
            if (value.IsNull()) return value;
            char[] chars = value.ToArray();
            for (int i = 0; i < chars.Count(); i++)
            {
                if (i == 0) continue;
                if (char.IsUpper(chars[i - 1]) && char.IsUpper(chars[i]))
                {
                    return chars[i - 1] + chars[i].ToString();
                }
            }
            return "";
        }

        /// <summary>
        /// 验证IP
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsIP(this string value)
        {
            return Regex.IsMatch(value, @"\b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\b");
        }

        /// <summary>
        /// 字符串提取IP
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetIP(this string value)
        {
            Match m = Regex.Match(value, @"((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)");
            return m.Value;
        }

        /// <summary>
        /// 验证域名
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsDomain(this string value)
        {
            return Regex.IsMatch(value, @"^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$");
        }

    }
}
